#include "main.h"

// ListInitiate
int ListInitiate(SLNode **phead)
{
    (*phead) = malloc(sizeof(SLNode));
    if (*phead == NULL)
    {
        return 1;
    }
    (*phead)->next = NULL;
    (*phead)->data = 0;
    return 0;
}

void ListInitNode(SLNode **pn, DataType e)
{
    (*pn) = malloc(sizeof(SLNode));
    (*pn)->next = NULL;
    (*pn)->data = e;
}

int ListIsEmpty(SLNode *head)
{
    return head->next == NULL;
}

// ListLength
int ListLength(SLNode *head)
{
    if (ListIsEmpty(head))
    {
        return 0;
    }
    int j = 0;
    while (!ListIsEmpty(head))
    {
        head = head->next;
        j++;
    }
    return j;
}

SLNode *ListGetNode(SLNode *head, int i)
{
    i++;
    int j = 1;
    SLNode *p = head;
    while (p && j <= i)
    {
        p = p->next;
        j++;
    }
    return p;
}

DataType ListGetEle(SLNode *head, int i)
{
    return ListGetNode(head, i)->data;
}

SLNode *ListGetNext(SLNode *head, int i)
{
    return ListGetNode(head, i)->next;
}

void ListChangeNext(SLNode **ppL1, int i, SLNode *pL2)
{
    static int j = 0;
    if (j == 0)
    {
        i++;
    }
    j++;
    if (j <= i)
    {
        ListChangeNext(&((*ppL1)->next), i, pL2);
    }
    if (j > i)
    {
        (*ppL1)->next = pL2;
        j--;
    }
    j = 0;
}

void insert_LinkList(SLNode **phead, int i, DataType e)
{
    SLNode *node_e, *pnode_i;
    ListInitNode(&node_e, e);
    pnode_i = ListGetNode(*phead, i);
    ListChangeNext(phead, i - 1, node_e);
    ListChangeNext(phead, i, pnode_i);
}

// ListInsert
int ListInsert(SLNode *head, int i, DataType x)
{
    int j = ListLength(head);
    insert_LinkList(&head, i, x);
    if (j == ListLength(head))
    {
        return 1;
    }
    return 0;
}

void delete_LinkList(SLNode **phead, int i)
{
    SLNode *pnode_i;
    pnode_i = ListGetNode(*phead, i);
    ListChangeNext(phead, i - 1, pnode_i->next);
    free(pnode_i);
}

// ListDelete
int ListDelete(SLNode *head, int i, DataType *x)
{
    *x = ListGetEle(head, i);
    delete_LinkList(&head, i);
    return 0;
}

// ListGet
int ListGet(SLNode *head, int i, DataType *px)
{
    *px = ListGetEle(head, i);
    return 0;
}

// Destroy
int Destroy(SLNode **phead)
{
    if (ListIsEmpty(*phead) == 1)
    {
        printf("the last print %p\n", (*phead)->next);
        // free(*phead);
        // (*phead)->next = NULL;
        *phead = NULL;
        printf("destroyed successed");
        return 0;
    }
    else
    {
        delete_LinkList(phead, 0);
        Destroy(phead);
    }
    return 0;
}

void print_LinkList(SLNode *head)
{
    printf("Printing LinkList:\n");
    printf("No.\t Position\t\t Value1\t Getval\t Next\t \n");
    int j = ListLength(head), x = 0;
    for (int i = 0; i < j; i++)
    {
        ListGet(head, i, &x);
        printf("%d\t %p\t %d\t %d\t %p\t \n",
               i, ListGetNode(head, i), ListGetEle(head, i), x, ListGetNext(head, i));
    }
}

int main(void)
{
    SLNode *head;
    int i, x;
    ListInitiate(&head); /*初始化*/
    printf("length %d\n", ListLength(head));
    for (i = 0; i < 10; i++)
    {
        ListInsert(head, i, i + 1); /*插入10个数据元素*/
    }
    printf("length %d\n", ListLength(head));
    if (ListDelete(head, 4, &x) == 0)
    { /*删除数据元素5*/
        printf("x: %d \n", x);
    }
    printf("length %d\n", ListLength(head));
    print_LinkList(head);
    Destroy(&head);
    return 0;
}